package io.gitee.xyuesheng.day01;

import org.junit.Test;

public class TestLoop {
    // 循环的终止条件

    @Test       // 单元测试
    public void testWhile() {
        int count = 10;

        while (count > 0) {
            count--;

            System.out.println(count);
        }
    }

    @Test
    public void testDoWhile() {
        int count = 10;
        do {
            count--;
            System.out.println(count);
        } while (count > 0);
    }

    @Test
    public void testDice() {
        int count = 0;
        while (true) {
            int number1 = (int) (Math.random() * 6) + 1;
            int number2 = (int) (Math.random() * 6) + 1;

            count++;

            System.out.println("第" + count + "次");

            if (number1 == 6 && number2 == 6) {
                // + 字符串拼接
                // 操作符重载，String可以直接用+号
                System.out.println("number1:" + number1 + " number2:" + number2);

                break;  // 跳出循环
            }
        }
    }

    @Test
    public void testFor() {
        // 初始化语句
        // 循环条件
        // 递增操作
        for (int i = 1; i <= 10; i++) {
            System.out.println(i);
        }

        for (int i = 2; i <= 16; i += 2) {
            System.out.println(i);
        }
    }

    @Test
    public void testPrint1() {
        for (int j = 1; j <= 3; j++) {
            for (int i = 1; i <= 10; i++) {
                System.out.print("* ");
            }

            System.out.println();
        }
    }

    @Test
    public void testPrint2() {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }

            System.out.println();
        }
    }

    /**
     * -1,1	-1,0 -1,-1
     * 0,1	0,0	 0,-1
     * 1,1	1,0	 1,-1
     */
    @Test
    public void testPrint3() {
        for (int i = -1; i <= 1; i++) {
            for (int j = 1; j >= -1; j--) {
                System.out.print(i + "," + j + "\t\t");
            }

            System.out.println();
        }
    }

    // 素数，质数
    @Test
    public void testPrime() {
        int i = 102;

        for (int j = 2; j < i; j++) {
            if (i % j == 0) {
                System.out.println("不是素数");

                break;  // 跳出当前循环
            }
        }
    }

    // 报数游戏
    @Test
    public void testNumber7() {
        int count = 1;
        while (true) {
            if (count % 7 != 0) {
                System.out.println(count);
            } else {
                System.out.println("过");
            }

            count++;

            if (count >= 100) {
                break;
            }
        }
    }

    // 报数游戏
    @Test
    public void testNumber72() {
        int count = 0;
        while (true) {
            count++;

            if (count % 7 == 0) {
                System.out.println("过");
                continue;       // 跳过当前循环后面的所有代码，进入下次循环
            }

            System.out.println(count);

            if (count >= 100) {
                break;
            }
        }
    }

    @Test
    public void testPrint4() {
        out:
        for (int i = -1; i <= 1; i++) {
            for (int j = 1; j >= -1; j--) {
                System.out.print(i + "," + j + "\t\t");

                if (j == 0 && i == 0) {
                    // break;       break: 只能跳出内层循环

                    // return;
                    break out;      // 定义一个标签，跳出标签处的循环
                }
            }

            System.out.println();
        }
    }
}
